import http from "../../../class/Utils/Http"
import tips from "../../../class/Utils/Tips"
import user from "../../../class/Utils/User"
var app = getApp();
Page({
    data: {
        aa: 0,
        sctig: 0,//靠谱状态 1 靠谱+1
        kpshu: 121, //靠谱状态数,
        piclist: [],
        bofang: 0,
        bb: 0,
        yuyinqianmingtig: 0,
        openinfo: false,
        mykehulist:[],
        xiaoxi_count: 0,
    },
    // onLoad: function (get) {
    //     var that = this
    //     var user = wx.getStorageSync('user');
    //     that.setData({
    //         qyid: get.qyid || 1
    //     })
    //     console.log(user)
    //     if (!user) {
    //         that.setData({
    //             shouquan: 1
    //         })
    //     } else {
    //         that.setData({
    //             shouquan: 0
    //         })
    //         wx.showLoading({
    //             title: '加载中...',
    //         })
    //
    //         that.infomingpian()
    //         that.stopmuqianming()
    //     }
    //
    //
    // },
    onLoad: function (get) {
        var that = this
        var user = wx.getStorageSync('user');
        that.setData({
            qyid: get.qyid || 1
        })

    },
    onShow: function () {
        var that = this
        var user = wx.getStorageSync('user');
        wx.removeStorageSync('pic_post_data')

        //console.log(user)
        if (!user) {
            that.setData({
                shouquan: 1
            })
        } else {
          
            that.setData({
                shouquan: 0
            })
            wx.showLoading({
                title: '加载中...',
                mask:true
            })

            that.infomingpian()
          //  that.toocle_car();
            that.stopmuqianming()
            
        }

        /*
         获取未读消息数量
         */
        http.req('/Mingpian/xiaoxi', { uid: user.userid }, function (res) {
            console.log(res)
            that.setData({
                xiaoxi_count: res
            })
        })

      wx.onBackgroundAudioStop(function () {
        console.log('onBackgroundAudioStop')
        that.setData({
          bofang: '0',
          yuyinqianmingtig: 0,
        })
      })

      
    },
    /*
    toocle 总部的名片数据
     */
    toocle_car:function () {
        var that = this
        var user = wx.getStorageSync('user');
        http.req("/Api/toocle_mingpian_info_wx",{uid:user.userid},function (res) {
                console.log(res)
        })
    },
    /*
     本库的网站数据
     */
    infomingpian: function () {
        var that = this
        var user = wx.getStorageSync('user');
      //console.log(user)
        that.setData({
            user: user
        })
        var data = {
            userid: user.userid,
            qyid: that.data.qyid
        }
        //console.log(data)
        http.req("/Mingpian/info", data, function (infores) {
          console.log(infores)

            if (infores.s == 1) {
                that.setData({
                    sctig: infores.sc,
                    sc_num: infores.dianzan,
                  mpinfo: infores.id
                })
                var content_pic = infores.content_pic;
                var piclist = []
                if (content_pic.length > 0) {
                    for (var i = 0; i < content_pic.length; i++) {
                        piclist.push(content_pic[i].pic)
                        that.setData({
                            piclist: piclist
                        })
                    }
                }
                that.setData({
                    info: infores,
                })
              that.wodekehulist()
            }
            if (infores.xingming || infores.zhiwei || infores.gongsi || infores.shouji) {
                that.setData({
                    s: 1
                })
            } else {
                that.setData({
                    s: 2
                })
            }
            setTimeout(function () {
                wx.hideLoading()
            }, 1000)


        })
      
    },
    //获取我的客户数据
    wodekehulist:function(){
      var that = this
      var user = wx.getStorageSync('user');
      //获取我的客户数据
        var data = {
            mid: that.data.mpinfo,
        }
        console.log(data)
        http.req("/Mingpian/liulan_renyuan", data, function (res) {
            console.log(res)
            that.setData({
                renqi:res.renqi,
                mykehulist:res.list
            })

        })

    },
    //下拉刷新
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()
        wx.showLoading({
            title: '更新信息...',
        })

        var that = this;

        var user = wx.getStorageSync('user');
        var userid = user.userid

        that.setData({
            user: user,
            piclist: []
        })
        http.req("/Mingpian/info", {userid: userid}, function (infores) {
            console.log(infores)
            if (infores.s == 1) {
                that.setData({
                    sctig: infores.sc,
                    sc_num: infores.dianzan
                })
                var content_pic = infores.content_pic;
                var piclist = that.data.piclist
                if (content_pic.length > 0) {
                    for (var i = 0; i < content_pic.length; i++) {
                        piclist.push(content_pic[i].pic)
                        that.setData({
                            piclist: piclist
                        })
                    }
                }
                that.setData({
                    info: infores,
                    s: 1
                })
            } else {
                that.setData({
                    s: 2,
                    bb: 1
                })
            }

        })


        wx.stopPullDownRefresh()
        wx.hideNavigationBarLoading()
        setTimeout(function () {
            wx.hideLoading()
        }, 1000)


    },
    previewImage: function (e) {
        var that = this
        var current = e.currentTarget.dataset.src;
        wx.previewImage({
            current: current,// 当前显示图片的http链接
            urls: that.data.piclist // 需要预览的图片http链接列表
        })
    },
    //添加到通讯录
    adddianhuabu: function () {
        var that = this
        wx.addPhoneContact({
            //lastName: that.data.lastName,
            photoFilePath: that.data.info.pic,//头像
            firstName: that.data.info.xingming,//联系人姓名
            organization: that.data.info.gongsi,//公司名称
            title: that.data.info.zhiwei,//公司职位
            mobilePhoneNumber: that.data.info.shouji,//联系人手机号
            weChatNumber: that.data.info.weixin,//微信号
            email: that.data.info.youxiang,//电子邮箱
            workAddressCity: that.data.info.dizhi,//工作城市
            workPhoneNumber: that.data.info.guhua,//办公电话
        })
    },
    //播放语音个人签名
    playyuyinqianming: function () {
        var that = this
        var yyqmtig = that.data.bofang
        console.log(yyqmtig)
        if (yyqmtig == 0) {
            wx.playBackgroundAudio({
                dataUrl: that.data.info.qianmingmp3,
                title: '个性签名',
            })
            that.setData({
                bofang: 1,
                yuyinqianmingtig: 1,
            })

        }
        if (yyqmtig == 1) {
            wx.stopBackgroundAudio()
            that.setData({
                bofang: '0',
                yuyinqianmingtig: 0,
            })
            console.log(yyqmtig)
        }


    },
    //跳转页面 停止播放
    onHide: function () {
        var that = this
        wx.stopBackgroundAudio()
        that.setData({
            bofang: '0',
        })
    },
    //自动停止播放语音签名
    stopmuqianming: function () {
        var that = this
        wx.onBackgroundAudioStop(function () {
            that.setData({
                yuyinqianmingtig: 0
            })
        })
    },
    //播放语音签名
    playyuyinqianmingb: function () {
        var that = this
        wx.playBackgroundAudio({
            dataUrl: that.data.yuyinqianming,
            title: '个性签名',

            success: function () {
                wx.getBackgroundAudioPlayerState({
                    success: function (res) {
                        var status = res.status
                        if (status == 1) {
                            wx.stopBackgroundAudio()
                            that.setData({
                                bofang: '0',
                            })
                        }
                    }
                })
            }

        })
    },
    //点击加减靠谱
    aabb: function () {
        var that = this
        var user = wx.getStorageSync('user');
        var sc_num = that.data.sc_num
        var stig = that.data.sctig//1是靠谱 0是没选
        var kptig = that.data.kpshu//靠谱当前的数
        var aatig = that.data.aa// 1是运行红心动画 0是隐藏红心动画
        if (stig == 0) {
            that.setData({
                aa: 1,
                sctig: 1,
                jia1: '+1',
                kpshu: kptig + 1,
                sc_num: parseInt(sc_num) + 1

            })
            setTimeout(function () {
                that.setData({
                    aa: 0,
                })
            }, 1000)
            http.req("/Mingpian/mp_sc", {userid: user.userid, id: that.data.info.id, title: that.data.info.xingming})
        } else {
            that.setData({
                aa: 1,
                sctig: 0,
                jia1: '-1',
                kpshu: kptig - 1,
                sc_num: parseInt(sc_num) - 1
            })
            setTimeout(function () {
                that.setData({
                    aa: 0,
                })
            }, 1000)
            http.req("/Mingpian/del_mp_sc", {userid: user.userid, id: that.data.info.id})
        }

    },
    onShareAppMessage: function () {
        var that = this
        return {
            title: '我是' + that.data.info.gongsi + "的" + that.data.info.xingming,
            path: '/pages/mingpian/card/card?id=' + that.data.info.id,
            imageUrl: that.data.info.pic,

            success: function (res) {
                http.req("/Mingpian/fenxiang", {id: that.data.info.id}, function (res) {

                    tips.ok("转发成功")

                })
            },
        }

    },
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function () {
            that.setData({
                shouquan: 0
            })
            wx.showLoading({
                title: '加载中...',
            })
            that.infomingpian()
        })
    },
    //改变名片展开信息
  changempinfo:function(){
    var that = this
    that.setData({
      openinfo: !that.data.openinfo
    })
  },
//去首页
    gotohome:function(){
        wx.switchTab({
            url: '/pages/index/index'
        })
    },
    //去我的
    gotomain: function () {
        wx.switchTab({
            url: '/pages/main/menu/menu'
        })
    },
    //去名片
    gotowodemingpian:function(e){
        wx.redirectTo({
            url: '/pages/mingpian/card/card?id='+this.data.info.id
        })
    },
    //去旺铺
    gotowangpu:function(){
        var that = this
        var info = this.data.info
        if(info.wx_user.toocle_cid == 0){
            tips.error("暂无旺铺")
            return false
        }
        // if(info.wx_user.toocle_cid==100387){
        //     tips.redirectTo('/pages/mingpian/wangpu/index/index?mid='+this.data.info.id);
        // }else{
      tips.redirectTo("/pages/toocle/shop/index/index?cid=" + info.wx_user.toocle_cid + "&time=" + info.wx_user.toocle_ctime + "&quanmenuid=1&mid=" + that.data.info.id + "&menus=2&userid="+info.wx_user.id);
        // }

    },
    //去企业介绍
    gotojieshao:function(){
        var info = this.data.info
        if(info.wx_user.toocle_cid == 0){
            tips.error("暂无旺铺")
            return false
        }

       tips.redirectTo("/pages/toocle/shop/index/index?cid="+info.wx_user.toocle_cid+"&time="+info.wx_user.toocle_ctime+"&quanmenuid=3&userid="+info.wx_user.id);

    },
    //去生意宝服务中心123123
    // gotofuwuzhongxin:function(){
    //     tips.redirectTo('/pages/mingpian/tooclefuwu/tooclefuwu');
    // },
  //去生意宝服务中心
  gotofuwuzhongxin: function () {
    tips.godaohang('/pages/main/menu/menu');
  },
    //拨打手机
    callshouji: function () {
      var that = this

      if (that.data.info.shouji !== ''){
        wx.makePhoneCall({
          phoneNumber: that.data.info.shouji,
          success: function () {
            console.log("成功拨打电话")
          }
        })
      }

      
    },
    //拨打固话
    callguhua: function () {
      var that = this
      if (that.data.info.guhua !== '') {
        wx.makePhoneCall({
          phoneNumber: that.data.info.guhua,
          success: function () {
            console.log("成功拨打电话")
          }
        })
      }
      
    },
  //复制微信号
  copyweixin: function (e) {
    var that = this;
    var thatweixinhao = that.data.info.weixin
    console.log(thatweixinhao)


    if (thatweixinhao == '' || thatweixinhao == 'undefined' || thatweixinhao == 'unll') {

    } else {

      wx.setClipboardData({
        data: that.data.info.weixin,
        success: function (res) {
          // that.setData({copyTip:true}),

          wx.showModal({
            title: '提示',
            content: '复制成功',
            showCancel: false,
            // success: function (res) {
            //   if (res.confirm) {
            //     console.log('确定')
            //   } else if (res.cancel) {
            //     console.log('取消')
            //   }
            // }
          })

          console.log("success" + res.data)
        },
        // fail: function (res) {
        //   wx.showModal({
        //     title: '提示',
        //     content: '没有微信号',
        //     showCancel: false,
        //     success: function (res) {
        //       if (res.confirm) {
        //         console.log('确定')
        //       } else if (res.cancel) {
        //         console.log('取消')
        //       }
        //     }
        //   })
        // }
      });
    }





  },
  selectaddfangshi:function(){
    var that = this
    wx.showModal({
      //title: "温馨提示",
      content: '选择编辑方式',
      cancelText: "手动编辑",//默认是“取消”
      confirmText: "拍摄名片",//默认是“确定”
      // confirmColor: '#586fbd',//确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
          tips.goto('/pages/mingpian/add/add')
        } else {
          that.upimg()
          
        }
      },
    })
  },
  //牛逼的扫描名片功能 嘿嘿嘿
  upimg: function () {
    var that = this;
    
    wx.chooseImage({
      count: 1,
      success: function (res) {
        tips.loading("图片处理中..")
        //console.log(res)
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: app.httpurl.url + '/Api/mingpianshibie',
          filePath: tempFilePaths[0],
          name: 'pic',
          success: function (result) {
            const res = JSON.parse(result.data)
            console.log(res)
            if (res.code === "0") {
              var post_data = {}
              if (res.data.organization) {
                post_data.gongsi = res.data.organization['0'].item.name
              }
              if (res.data.formatted_name) {
                post_data.xingming = res.data.formatted_name['0'].item
              }
              if (res.data.label) {
                post_data.dizhi = res.data.label['0'].item.address
              }
              if (res.data.title) {
                post_data.zhiwei = res.data.title['0'].item
              }
              if (res.data.telephone) {
                post_data.shouji = res.data.telephone['0'].item.number
              }
              if (res.data.telephone.length > 1) {
                post_data.guhua = res.data.telephone['1'].item.number
              }
              if (res.data.email) {
                post_data.youxiang = res.data.email['0'].item
              }
              console.log(post_data)
              wx.setStorageSync('pic_post_data', post_data)
              tips.goto("/pages/mingpian/add/add?pic_post_data=1")
            } else {
              // tips.error("识别识别，请保证名片清晰!")
              
              wx.showModal({
                // title: "温馨提示",
                content: '识别失败，请保证名片拍摄清晰哦!',
                cancelText: "我知道了",//默认是“取消”
                confirmText: "重新拍摄",//默认是“确定”
                // confirmColor: '#586fbd',//确定文字的颜色
                success: function (res) {
                  if (res.cancel) {
                    //点击取消,默认隐藏弹框
                  } else {
                    that.upimg()

                  }
                }
              })
              tips.loaded()





            }

          }
        })
      }
    })
  },

})